package com.junxian.demo02;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Demo02Application.class)

public class Demo02ApplicationTests02 {
    @Autowired
    @Qualifier("primaryJdbcTemplate")//指定注入Bean的名称, 结合@Autowired使用时，自动注入的策略就从byType转变成byName了
    protected JdbcTemplate jdbcTemplate01;

    @Autowired
    @Qualifier("secondaryJdbcTemplate")
    protected JdbcTemplate jdbcTemplate02;

    @Before
    public void setUp(){
        jdbcTemplate01.update("DELETE from USER ");
        jdbcTemplate02.update("DELETE from USER");
    }

    @Test
    public void test() throws Exception{
        jdbcTemplate01.update("INSERT into USER (id , name ,age) VALUES (? , ? , ?)" , 1 , "aaa" , 10);
        jdbcTemplate01.update("INSERT into USER (id , name ,age) VALUES (? , ? , ?)" , 2 , "bbb" , 20);

        jdbcTemplate02.update("INSERT into USER (id , name ,age) VALUES (? , ? , ?)" , 2 , "ccc" , 30);

        System.out.println("jdbcTemplate01的数量：" + jdbcTemplate01.queryForObject("select count(1) from user" , String.class));
        System.out.println("jdbcTemplate02的数量：" + jdbcTemplate02.queryForObject("select count(1) from user" , String.class));

    }

}